package com.app.web.merchant;

import com.app.utils.base.http.ResultUtil;
import com.app.utils.base.j2se.Common;
import com.app.utils.base.j2se.Convert;
import com.app.utils.base.j2se.JsonUtil;
import com.app.web.goods.goods.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Description
 * Created by 王伟
 * Date create on 10:20 2019/11/26.
 */
@RestController
@RequestMapping("merchant/")
public class MerchantController {
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private GoodsService goodsService;

    /**
     * 根据商家ID查询基本信息
     * @param id
     */
    @RequestMapping("{id}")
    public String merchant(@PathVariable int id){
        Map<String,Object> data = MerchantManager.getMerchantById(id);
        data.put("cats",hosCatsStr(id,0));
        return ResultUtil.success(data);
    }

    /**
     * 查询商家列表
     * @param name  商家名
     * @param isRecommend  是否严选
     * @param page  分页
     */
    @RequestMapping("list")
    public String merchantList(@RequestParam(defaultValue = "") String name,
                               @RequestParam(defaultValue = "0") int isRecommend,
                               @RequestParam(defaultValue = "1") int page){
        List<Merchant> data = MerchantManager.getMerchantList(name,isRecommend,page);
        if(Common.isEmpty(data))return ResultUtil.error(555,"暂无数据");
        return ResultUtil.success(data);
    }

    /**
     * 查询商家资质图
     * @param id
     * @return
     */
    @RequestMapping("pics")
    public String pics(int id){
        List<MerchantPics> data = merchantService.merchantPics(id);
        if(Common.isEmpty(data))return ResultUtil.error(555,"暂无数据");
        return ResultUtil.success(data.stream().map(MerchantPics::toMap).collect(Collectors.toList()));
    }

    /* 根据商家ID 获取并组装商家特长项目类型 */
    List<Map<String,Object>> hosCatsStr(Object hosId,int count){
        return goodsService.goodsCategoryByType(Convert.objToInt(hosId), 1,count).stream().map(JsonUtil::beanToMap).collect(Collectors.toList());
    }

    @RequestMapping("expressInfo")
    public String expressInfo(int id){
        Map<String,Object> data = new HashMap<String,Object>(){{
            put("name","刘晓红");
            put("phone","13896172808");
            put("address","重庆市合川区");
        }};
        return ResultUtil.success(data);
    }
}
